package com.avicit.zipkinkafkaesserver.commands;

import com.avicit.zipkinkafkaesserver.enums.BatchNumberTypeEnum;
import com.avicit.zipkinkafkaesserver.factory.BatchNumberFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 批次号计数器
 */
public class BatchNumberContext {

    private final Map<BatchNumberTypeEnum, BatchNumberFactory> factoryMap = new ConcurrentHashMap<>();
    private  static volatile  BatchNumberContext batchNumberContext;
    private BatchNumberContext() {
        final BatchNumberFactory zipkinInterfaceCount = new BatchNumberFactory();
        final BatchNumberFactory zipkinInterfaceAverageDuration = new BatchNumberFactory();
        //分别对应一个计数器
        factoryMap.put(BatchNumberTypeEnum.ZipkinInterfaceCount, zipkinInterfaceCount);
        factoryMap.put(BatchNumberTypeEnum.ZipkinInterfaceAverageDuration, zipkinInterfaceAverageDuration);
    }

    /**
     * 单例
     * @return
     */
    public static BatchNumberContext batchNumberContext() {
        if (batchNumberContext == null) {
            synchronized (BatchNumberContext.class) {
                if (batchNumberContext == null) {
                    batchNumberContext = new BatchNumberContext();
                }
            }
        }
        return batchNumberContext;
    }

    /**
     * 根据类型获取 各自的计算器
     * @param batchNumberTypeEnum
     * @return
     */
    public BatchNumberFactory batchNumberFactory(BatchNumberTypeEnum batchNumberTypeEnum) {
        return factoryMap.get(batchNumberTypeEnum);
    }
}
